package com.zzj.controller;

import com.zzj.base.Result;
import com.zzj.entity.Barcodes;
import com.zzj.service.IBarcodesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 标准商品条码表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2022-12-13
 */
@RestController
public class BarcodesController {

    @Resource
    private IBarcodesService barcodesService;

    @Operation(summary = "获取所有条形码", description = "返回条形码列表", method = "GET")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Barcodes.class)))
    @PostMapping("queryByBarcode")
    public Result<Barcodes> queryByBarcode(@RequestParam("barcode") String barcode) {

        List<Barcodes> list = barcodesService.lambdaQuery().eq(Barcodes::getBarcode, barcode).list();
        if (list.isEmpty()) {
            return Result.error("当前条码商品不在条形码数据库中");
        } else {
            return Result.success(list.get(0));
        }
    }
}
